package net.slashie.libjcsi.textcomponents;

import net.slashie.libjcsi.ConsoleSystemInterface;

public class BarGraph extends TextComponent {
	private static final String TWO = (char) 9608 + "";
	private static final String ONE = (char) 9604 + "";
	private static final String ZERO = " ";

	private static final long serialVersionUID = -3020236984353127517L;
	private int maxValue;
	private int value;

	public BarGraph(ConsoleSystemInterface csi) {
		super(csi);
	}

	@Override
	public void draw() {
		clearBox();
		drawBorder();
		
		int ticksToPaint = getTicks();
		for (int i = position.y + getHeight() - 1; position.y < i; i--) {
			if (ticksToPaint > 1) {
				si.print(position.x + 1, i, TWO, getForeColor());
				ticksToPaint -= 2;
			} else if (ticksToPaint > 0) {
				si.print(position.x + 1, i, ONE, getForeColor());
				ticksToPaint -= 1;
			} else {
				si.print(position.x + 1, i, ZERO, getForeColor());
			}
		}
	}

	private int getTicks() {
		return (int) ((((double) getHeight() - 1) * 2) * ((double) value / maxValue));
	}

	public void setMaximum(int maxValue) {
		this.maxValue = maxValue;
	}

	public void setValue(int value) {
		this.value = value;
	}
}
